package com.haoziqi.chapter_01;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 *  TODO 流处理下的批处理
 * description
 * created by A on 2021/3/2
 */
public class BoundedStream02 {
    public static void main(String[] args) throws Exception {
        //1.创建执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //1,1加上这个设置就可以实现流批一体化
        env.setRuntimeMode(RuntimeExecutionMode.BATCH);
        //2.读取文本数据
        DataStreamSource<String> ds = env.readTextFile("input/word.txt");
        //3.处理文本数据  flatMap接口的第一个参数执行输入类型,第二个参数指定输出类型,SingleOutputStreamOperator存储输出的数据集
        SingleOutputStreamOperator<String> oso = ds.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String s, Collector<String> collector) throws Exception {
                String[] strs = s.split(" ");
                for (String str : strs) {
                    collector.collect(str);
                }
            }
        });
        //4.map变换数据格式为(hello,1)
        //MapFuntion接口的第一个参数规定了传入的类型,第二个参数规定了输出的类型
        SingleOutputStreamOperator<Tuple2<String, Long>> map = oso.map(new MapFunction<String, Tuple2<String, Long>>() {
            @Override
            public Tuple2<String, Long> map(String s) throws Exception {
                return Tuple2.of(s, 1L);
            }
        });
        //5.对数据进行聚合操作
        //根据元组的第0个元素将数据进行分组
        KeyedStream<Tuple2<String, Long>, Tuple> ks = map.keyBy(0);
        //6.对数据进行聚合操作
        SingleOutputStreamOperator<Tuple2<String, Long>> sum = ks.sum(1);
        //打印数据
        sum.print();
        //启动执行
        env.execute();
    }
}
